	$(document).ready(function(){		  
		bindProductSelectHandler();

		$("#btn_post").click(function(){
			post_order();
		});	

	});


	function post_order() {
		if($("#truename").val()=="" || $("#address").val()=="") {
			alert("请填写您的真实姓名及收货地址！");
			return false;
		}
		if($("#mobile").val().length != 11) {
			//alert("您的手机号码填写有误，请确认！");
			//return false;
		}
		if(confirm("确认要提交定单吗？")) {
			var mobile = $("#mobile").val();
			var params = {
				"type" : type,
				"count" : count,
				"box" : box,
				"price" : price,
				"total" : total,
				"truename" : $("#truename").val(),
				"mobile" : mobile,
				"area" : $("#area").val(),
				"address" : $("#address").val(),
				"content" : $("#content").val(),
				"order_source" : $("#order_source").val(),
				"product" : $("#product").val()
			}
			$("#btn_post").hide();
			$("#post_waiting").show();
			$.post("order_post.php", params, function(data){
				if(data == "ok") {
					alert("您的定单已经成功提交！我们将尽快为您安排配送，感谢您的惠顾！");
					window.location = "order.php";
				} else if(data == "mobile_is_exists") {
					if( confirm("您的手机["+mobile+"]已经在本站购物过，需要先登录后再进行操作吗？")) {
						window.location = "login.php"
					}
				} else {
					alert("[定单提交失败] >>> "+data);
					//alert("定单提交失败，请重试！");
				}
				$("#btn_post").show();
				$("#post_waiting").hide();
			});
		}
	}

	function bindProductSelectHandler() {
		$("#product_select a").click(function(){
			event.cancelBubble = true;
			change_product_select($(this));
			return false;
		});	
	}

	function change_product_select(a) {
		var g = a.attr('group');
		var v = a.html();
		$("a[group='"+g+"']").each(function(){
			$(this).addClass("ps");
			$(this).removeClass("ps_select");
		});
		a.addClass("ps_select");
		a.blur()
		calcute_price_total(g, v); // 更新计算单价及总价
	}


	/*
	// 移到外围做动态判断，2014/11/19
	var type = "小";
	var count = "6"; // count = "2对";
	var box = "标准";
	var price = "15";
	var total = "60"
	*/

	function calcute_price_total(g, v) {
		eval(g+" = '"+v+"'");
		price = type_price[type];
		total = price * parseInt(count) + box_price[box]; // count_arr[count]
		$("#price").html(price);
		$("#total").html(total);
	}

	var type_price = {"小": 15, "中":20, "大":25};
	//var count_arr = {"2对": 4, "4对": 8, "6对": 12, "8对": 16};
	var box_price = {"标准":0, "礼品袋":8};
